import TileLayer from 'ol/layer/Tile';
import XYZ, { type Options } from 'ol/source/XYZ';
// @ts-ignore
import { TileSuperMapRest } from '@supermap/iclient-ol';
// import VectorTileLayer from 'ol/layer/VectorTile';
// import VectorTileSource from 'ol/source/VectorTile';
// import MVT from 'ol/format/MVT';
// import TileGrid from 'ol/tilegrid/TileGrid';
// import Feature from 'ol/Feature';

const tdtTK = '464554f64aa4f4e90e0321c17a57a331';
const projection = 'EPSG:4326';

/**
 * 创建天地图图层
 * @param optiosn
 * @returns TileLayer
 */
export const createTdtLayer = (optiosn: Options) => {
  // t = 'vec_c'
  const url = `http://t{0-7}.tianditu.com/DataServer?T=vec_c&x={x}&y={y}&l={z}&tk=${tdtTK}`;
  return new TileLayer({
    source: new XYZ({
      projection,
      ...optiosn,
      url: optiosn.url ? `${optiosn.url}&tk=${tdtTK}` : url,
    }),
  });
};

/**
 * 创建超图图层
 * @param optiosn
 * @returns TileLayer
 */
export const createSupermapLayer = (optiosn: Options) => {
  return new TileLayer({
    source: new TileSuperMapRest({
      projection,
      ...optiosn,
    }),
    // properties: {},
  });
};

const layerType = {
  // 天地图
  1: createTdtLayer,
  // 超图
  2: createSupermapLayer,
};

export type LayerType = keyof typeof layerType;

export type LayerOtions = {
  type: LayerType;
  name?: string;
  key?: string | number;
  options: Options;
};

export const createLayers = (config: LayerOtions) => {
  return layerType[config.type](config.options);
};
